package com.schoolCommunity.util;

import com.alibaba.fastjson.JSONArray;
import com.schoolCommunity.entity.Member;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Component
public class SearchES {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    //按照昵称，账号等搜索用户
    //传入昵称，邮箱，起始页数
    public List<Member> searchMember(String nickname, String familyName, int pageNo) throws IOException {

        if (pageNo == 0)
            pageNo = 1;
        if (nickname.equals(""))
            nickname = null;
        if (familyName.equals(""))
            familyName = null;

        List<Member> list = new ArrayList<>();

        //条件搜索
        //搜索的索引
        SearchRequest searchRequest = new SearchRequest("member");
        //创建搜索条件构造器
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//        先判断传来的数据
        //完全符合条件
        if (familyName != null && nickname != null) {
            System.out.println("姓氏昵称均正常");
            //构建查询条件
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                    //匹配查询条件
                    .must(QueryBuilders.matchQuery("nickname", nickname))
                    //可以设置多条匹配
                    .must(QueryBuilders.matchQuery("familyName", familyName))

            )
                    //设置分页查询，从0开始，查20条
                    .from(pageNo).size(20);
        } else if (familyName != null && nickname == null) {
            System.out.println("没有昵称，使用姓氏比较");
            //构建查询条件
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                            //匹配查询条件
//                    .must(QueryBuilders.matchQuery("nickname",nickname))
                            //可以设置多条匹配
                            .must(QueryBuilders.matchQuery("familyName", familyName))

            )
                    //设置分页查询，从0开始，查20条
                    .from(pageNo).size(20);
        } else if (familyName == null && nickname != null) {
            System.out.println("没有姓氏，使用昵称比较");
            //构建查询条件
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                            //匹配查询条件
                            .must(QueryBuilders.matchQuery("nickname", nickname))
                    //可以设置多条匹配
//                    .must(QueryBuilders.matchQuery("email",email))

            )
                    //设置分页查询，从0开始，查20条
                    .from(pageNo).size(20);
        } else {
            System.out.println("姓氏昵称都没有");
            return list;
        }


        //执行搜索
        searchRequest.source(searchSourceBuilder);
        //获取搜索结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析结果
//        List<Map<String,Object>> list = new ArrayList<>();

//        返回结果

        for (SearchHit documentFields : searchResponse.getHits().getHits()) {

            Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
            Object jsonO = JSONArray.toJSON(sourceAsMap);
            String json = jsonO.toString();
            Member member = JSONArray.parseObject(json, Member.class);
            list.add(member);

        }
        //返回member对象list
        return list;

    }


}
